package com.zhida.smartcontrol.pagectrl.lampmanager.doublelamp;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.jay.tools.recycler.MultipleViewHolder;
import com.jay.tools.recycler.SingleRecyclerAdapter;
import com.jay.tools.ui.ButtonStyle;
import com.jay.tools.ui.Cbox;
import com.jay.tools.utils.ResUtil;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampSwitchBean;
import com.zhida.smartcontrol.utils.MyUtils;

import java.util.List;

public class DoubleLampAdapter extends SingleRecyclerAdapter<LampSwitchBean> {
    private int mColorGreen, mColorRed;

    public DoubleLampAdapter(@Nullable List<LampSwitchBean> data) {
        super(R.layout.item_lamp_double, data);
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        Context context = recyclerView.getContext();
        mColorGreen = ResUtil.getColor(context, R.color.c_22D49C);
        mColorRed = ResUtil.getColor(context, R.color.c_FF4D62);
    }

    @Override
    protected void convert(MultipleViewHolder holder, LampSwitchBean info) {
        Cbox cbox = holder.getView(R.id.cbox);
        TextView tvControl = holder.getView(R.id.tv_control);
        TextView tvAddress = holder.getView(R.id.tv_address);
        Cbox cboxMain = holder.getView(R.id.cbox_main);
        ImageView ivMain = holder.getView(R.id.iv_main);
        TextView tvMain = holder.getView(R.id.tv_brightness_main);
        Cbox cboxAux = holder.getView(R.id.cbox_aux);
        ImageView ivAux = holder.getView(R.id.iv_aux);
        TextView tvAux = holder.getView(R.id.tv_brightness_aux);
        TextView tvJiaoshi = holder.getView(R.id.tv_jiao_shi);
        TextView tvTime = holder.getView(R.id.tv_time);

        cbox.setText(info.getImei());
        tvControl.setText(info.getControlType().equals(Const.AUTO) ? "自" : "手");
        tvAddress.setText(info.getAddress());

        tvJiaoshi.setText("校时：" + info.getClockUpdateTime());
        if (info.getUpdateTimeFlag().equals("0")) {
            // 显示绿色
            tvJiaoshi.setTextColor(mColorGreen);
        } else {
            tvJiaoshi.setTextColor(mColorRed);
        }

        tvTime.setText("更新：" + info.getUpdateDate());
        if (info.getUpdateStatus().equals("0")) {
            // 绿色
            tvTime.setTextColor(mColorGreen);
        } else {
            tvTime.setTextColor(mColorRed);
        }

        String brightNess = info.getBrightNess();
        if (TextUtils.isEmpty(brightNess)) {
            ivMain.setVisibility(View.GONE);
            ivAux.setVisibility(View.GONE);
            tvMain.setVisibility(View.GONE);
            tvAux.setVisibility(View.GONE);
        } else {
            ivMain.setVisibility(View.VISIBLE);
            ivAux.setVisibility(View.VISIBLE);
            tvMain.setVisibility(View.VISIBLE);
            tvAux.setVisibility(View.VISIBLE);
            tvMain.setText(MyUtils.getBrightNess(brightNess.substring(0,1)));
            tvAux.setText(MyUtils.getBrightNess(brightNess.substring(1)));
        }

        TextView tvDetail = holder.getView(R.id.tv_detail);
        ButtonStyle btnEdit = holder.getView(R.id.btn_edit);
        ButtonStyle btnDel = holder.getView(R.id.btn_del);

        cbox.setChecked(info.isCheck());
        cboxMain.setChecked(info.isCheckMain());
        cboxAux.setChecked(info.isCheckAux());

        cbox.setOnCboxClickListener(new MyCBoxClick(info, 0, holder.getAdapterPosition()));
        cboxMain.setOnCboxClickListener(new MyCBoxClick(info, 1, holder.getAdapterPosition()));
        cboxAux.setOnCboxClickListener(new MyCBoxClick(info, 2, holder.getAdapterPosition()));

        btnEdit.setOnClickListener(new MyClick(info, 0));
        btnDel.setOnClickListener(new MyClick(info, 1));
        tvDetail.setOnClickListener(new MyClick(info, 2));

    }

    private class MyCBoxClick implements Cbox.OnCboxClickListener {
        private LampSwitchBean mBean;
        private int tag;
        private int position;

        MyCBoxClick(LampSwitchBean bean, int tag, int position) {
            this.mBean = bean;
            this.tag = tag;
            this.position = position;
        }

        @Override
        public void onCboxClick(View v) {
            switch (this.tag) {
                case 0:
                    mBean.setCheck(!mBean.isCheck());
                    mBean.setCheckMain(mBean.isCheck());
                    mBean.setCheckAux(mBean.isCheck());
                    break;
                case 1:
                    mBean.setCheckMain(!mBean.isCheckMain());
                    if (mBean.isCheckMain() && mBean.isCheckAux()) {
                        mBean.setCheck(true);
                    } else {
                        mBean.setCheck(false);
                    }
                    break;
                case 2:
                    mBean.setCheckAux(!mBean.isCheckAux());
                    if (mBean.isCheckMain() && mBean.isCheckAux()) {
                        mBean.setCheck(true);
                    } else {
                        mBean.setCheck(false);
                    }
                    break;
            }
            notifyItemChanged(this.position);
        }
    }

    private class MyClick implements View.OnClickListener {
        private LampSwitchBean mBean;
        private int tag;

        MyClick(LampSwitchBean bean, int tag) {
            this.mBean = bean;
            this.tag = tag;
        }

        @Override
        public void onClick(View v) {
            if (null != mListener) {
                mListener.onClick(mBean, this.tag);
            }
        }
    }

}
